﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using SQLEditor.admin.entity;
using SQLEditor.dal;
namespace SQLEditor.admin.service
{
    public class OperateLogService : BaseService
    {
        
        public void add(string userName, string userIp, string action, bool success,string details) {
            tbOperateLog tb = new tbOperateLog
            {
                userName = userName,
                userIp = userIp,
                action  = action,
                success = success,
                description = details,
                operateDate = DateTime.Now,
            };
            DB.tbOperateLog.Add(tb);
            DB.SaveChanges();
        }

        public string getall()
        {
            var list = from b in DB.tbOperateLog                       
                       orderby b.operateDate descending
                       select new OperateLogEntity
                       {
                           id = b.id,
                           userName = b.userName,
                           userIp = b.userIp,
                           action = b.action,
                           success = b.success,
                           description = b.description,
                           operateDate = b.operateDate
                       };
            string json = JsonUtil.ToJson(list.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where)
        {
            int totalCount = 0;
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.tbOperateLog.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("tbOperateLog", "*", order, where, pageSize, pageIndex, paramTotalCount)).ToList();
            var list = from b in pager
                       orderby b.operateDate descending
                       select new OperateLogEntity
                       {
                           id = b.id,
                           userName = b.userName,
                           userIp = b.userIp,
                           action = b.action,
                           success = b.success,
                           description = b.description,
                           operateDate = b.operateDate
                       };
            var l = list.ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string delete(string strIds)
        {
            List<int> ids = new List<int>();
            if (!string.IsNullOrEmpty(strIds))
            {
                string[] arr = strIds.Split(',');
                foreach (var id in arr)
                {
                    ids.Add(int.Parse(id));
                }
            }
            var logs = DB.tbOperateLog.Where(m => ids.Contains(m.id)).ToList();
            if (logs != null)
            {
                logs.ForEach(m => DB.tbOperateLog.Remove(m));
                DB.SaveChanges();
            }
            return JsonUtil.ToJson(new { success = true });
        }
    }
}